package com.dude.screens;

import com.dude.search.ObjectDetectionView;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

/**
 *	this activity responsible for displaying the visual input to the user 
 */
public class ActiveSearchScreen extends Activity
{
	private static final String TAG = "search screen";

	
	// search mods
	public static final int     MATCH_TYPE_SYMMETRIC  = 0;
    public static final int     MATCH_TYPE_ASYMETRIC  = 1;
    
    // side menu view mode options
	private MenuItem optionsItem1;
	private MenuItem optionsItem2;
	
    // current view mode
    public static int matchThreshold = MATCH_TYPE_SYMMETRIC;

    // if true - this activity will open camera and generate live video.
    // if false - the video will be loaded from predefined path
    private boolean useCamera;
    
//    public static final SoundPool;
    
    //==========================================================================
    
    // activity startup
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //keep screen-saver off
        getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        //get user preferences
  		SharedPreferences settings = getSharedPreferences(Settings.settings, 0);
  		
  		// get user settings regarding video input, default is not using camera
  		useCamera = settings.getBoolean("use_camera", true);
        
        // display the camera input on screen
        setScreenVideoInput();
    }
	
	// activity disposal
	@Override
	public void onDestroy() 
	{
		// free bitmaps from memory
		FindObjectScreen.cleanBitmapsFromMemory();
		
		//release window lock
		getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		Log.i(TAG, "destroyed");
		super.onDestroy();
	}	
	
	
	// enable user to switch view modes (probably unnecessary)
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		optionsItem1 = menu.add("symmetric match");
		optionsItem2 = menu.add("asymmetric match");
        return true;
    }

	// change the current view mode (probably unnecessary)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == optionsItem1)
            matchThreshold = MATCH_TYPE_SYMMETRIC;
        else if (item == optionsItem2)
        	matchThreshold = MATCH_TYPE_ASYMETRIC;
        
        setMatchingType();
        
        return true;
    }
    
    private void setMatchingType() {
    	if (matchThreshold == MATCH_TYPE_SYMMETRIC)
    		ObjectDetectionView.isSym = true;
    	else if (matchThreshold == MATCH_TYPE_ASYMETRIC)
    		ObjectDetectionView.isSym = false;
	}

	// set the current screen video input according to user definition
    private void setScreenVideoInput()
    {
    	Log.i(TAG, "creating view...");
    	
    	// display camera
    	if (useCamera)
    	{
    		setContentView(new ObjectDetectionView(this, FindObjectScreen.bitmapsForSearch));
    	}
    	
		// display video from input file
    	else
    	{
    		
    	}
    }
}
